/**
 * @description 订单JS
 * @author 赵子靖
 * @since 2018-07-08
 */
var Order = function () {

    var t = {
        path: '',
        init: function () {
            t.initView();
            t.initEvent();

        },
        initView: function () {

        },
        initEvent: function () {

        },
        render: function () {
            $("#order-table").bootstrapTable('destroy');
            $("#order-table").bootstrapTable({
                queryParams: function (params) {
                    var sellerNicheng = $("#search_sellerNicheng").val();
                    var oSupplier = $("#search_oSupplier").val();
                    var oTicketNo = $("#search_oTicketNo").val();
                    var oName = $("#search_oName").val();
                    var oPnr = $("#search_oPnr").val();
                    var oNo = $("#search_oNo").val();
                    var oIssueDate = $("#search_issueDate").val();
                    
                    $.extend(params, {
                        sellerNicheng: sellerNicheng,
                        oSupplier: oSupplier,
                        oTicketNo: oTicketNo,
                        oName: oName,
                        oPnr: oPnr,
                        oNo: oNo,
                        oIssueDate:oIssueDate
                    });
                    return params;
                }
            });
            $("#order-table").bootstrapTable('refresh');

        },
        operator: function (value, row) {
            var html = "";
            html += '<a href="javascript:Order.updateOrderDialog(\'' + value + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp;';
            html += '<a href="javascript:Order.deleteOrderDialog(\'' + value + '\')" class="btn btn-default btn-xs">删除</a>';
            return html;
        },
        status: function (value, row) {
            if (value == 0) {
                return "创建";
            } else if (value == 1) {
                return "出票";
            } else if (value == 2) {
                return "退票";
            } else if (value == 3) {
                return "改期";
            } else if (value == 4) {
                return "废票";
            }
        },
        orderListDownload: function () {
            var sellerNicheng = $("#search_sellerNicheng").val();
            var oSupplier = $("#search_oSupplier").val();
            var oTicketNo = $("#search_oTicketNo").val();
            var oName = $("#search_oName").val();
            var oPnr = $("#search_oPnr").val();
            var oNo = $("#search_oNo").val();
            var oIssueDate = $("#search_issueDate").val();
            
            var paramter = "";
            if(sellerNicheng!=null && sellerNicheng!=''){
                paramter += "&sellerNicheng="+sellerNicheng;
            }
            if(oSupplier!=null && oSupplier!=''){
                paramter += "&oSupplier="+oSupplier;
            }
            if(oTicketNo!=null && oTicketNo!=''){
                paramter += "&oTicketNo="+oTicketNo;
            }
            if(oName!=null && oName!=''){
                paramter += "&oName="+oName;
            }
            if(oPnr!=null && oPnr!=''){
                paramter += "&oPnr="+oPnr;
            }
            if(oNo!=null && oNo!=''){
                paramter += "&oNo="+oNo;
            }
            if(oIssueDate!=null && oIssueDate!=''){
                paramter += "&oIssueDate="+oIssueDate;
            }
            
            window.location.href = t.path + "/order/orderListDownload.do?time="+new Date()+paramter;
        },
        //编辑订单
        updateOrderDialog: function (id) {
            $("#dialog").load(t.path + "/order/forwardUpdateOrderPost.do?id=" + id, function (responseTxt, statusTxt, xhr) {
                if (statusTxt == "success") {
                    $('#update-order-dialog-new').modal();
                    Order.fuzhi();
                    t.selectCustomer(id);
                } else if (statusTxt == "error") {
                    layer.alert("编辑失败", {
                        closeBtn: 0,
                        shift: 2 //动画类型
                    });
                }
            });
        },
        fuzhi: function () {
            var oSupplier = $("#lsoSupplier").val();
            $("#oSupplier").find("option[value='" + oSupplier + "']").attr("selected", "selected");
        },
        //删除订单
        deleteOrderDialog: function (id) {
            bootbox.confirm({
                size : 'small',
                message : "确认删除该订单，删除后将不可恢复？",
                callback : function(result) {
                    if (result) {
                        $.ajax({
                            type: "POST",
                            url: t.path + "/order/delOrderPost.do",
                            dataType: "text",
                            data: {
                                id: id
                            },
                            async: false,
                            cache: false,
                            success: function (data) {
                                alert(data);
                                $("#order-table").bootstrapTable('refresh');
                            }
                        });
                    }
                }
            });
        },
        //保存修改
        updateOrder: function () {

            var oId = $("#oId").val();
            var oNo = $("#oNo").val();
            var oPnr = $("#oPnr").val();
            var oCustomerId = $("#oCustomerId").val();
            var oRoute = $("#oRoute").val();
            var oTicketNo = $("#oTicketNo").val();
            var oName = $("#oName").val();
            var oTicketSource = $("#oTicketSource").val();
            var oSupplier = $("#oSupplier").val();
            var oStatus = $("#oStatus").val();

            var oTicketPrice = $("#oTicketPrice").val().replace(/,/, '');//票面价
            var oTicketRebate = $("#oTicketRebate").val().replace(/,/, '');//后返
            var oTicketTax = $("#oTicketTax").val().replace(/,/, '');//税金
            var oTicketSettlementPrice = $("#oTicketSettlementPrice").val().replace(/,/, '');//结算价
            var oTicketSellPrice = $("#oTicketSellPrice").val().replace(/,/, '');//实收价
            var oTicketProfit = $("#oTicketProfit").val().replace(/,/, '');//利润
            var oIssueDate = $("#oIssueDate").val();//出票日期
            var oRemark = $("#oRemark").val();//备注


            $.ajax({
                type: "POST",
                url: t.path + "/order/updateOrderPost.do",
                dataType: "text",
                data: {
                    oId: oId,
                    oNo: oNo,
                    oPnr: oPnr,
                    oCustomerId: oCustomerId,
                    oRoute: oRoute,
                    oTicketNo: oTicketNo,
                    oName: oName,
                    oTicketSource: oTicketSource,
                    oSupplier: oSupplier,
                    oTicketPrice: oTicketPrice,
                    oTicketTax: oTicketTax,
                    oStatus: oStatus,
                    oTicketSellPrice: oTicketSellPrice,
                    oTicketRebate: oTicketRebate,
                    oTicketSettlementPrice: oTicketSettlementPrice,
                    oTicketProfit: oTicketProfit,
                    oIssueDate:oIssueDate,
                    oRemark:oRemark
                },
                async: false,
                cache: false,
                success: function (data) {
                    alert(data);
                }
            });
            location.reload();
        },
        selectCustomer: function (id) {
            var data;
            $.ajax({
                type: "POST",
                url: t.path + "/customer/select2Customer.do",
                dataType: "json",
                data: {
                    id: id
                },
                async: false,
                cache: false,
                success: function (cutomer) {
                    data = cutomer;
                }
            });
            $("#oCustomerId").select2({
                data: data.results,
                placeholder: '请搜索客户...',
            });
        },
        getCustomerName: function (value) {
            var zz = value.split(',');
            return zz[1];
        },
        getoSeller: function (value) {
            var zz = value.split(',');
            return zz[1];
        },
        getJeSuanNum: function () {

            var oTicketPrice = $("#oTicketPrice").val().replace(/,/, '');//票面价
            var oTicketRebate = $("#oTicketRebate").val().replace(/,/, '');//后返
            var oTicketTax = $("#oTicketTax").val().replace(/,/, '');//税金

            //结算价=票价*（1-后返%）+税金
            var oTicketSettlementPrice = Math.ceil((oTicketPrice * (1 - oTicketRebate * 0.01)) + oTicketTax * 1);
            $("#oTicketSettlementPrice").val(oTicketSettlementPrice);
            t.getLiRunNum()
        },
        getLiRunNum:function () {
            var oTicketSettlementPrice = $("#oTicketSettlementPrice").val().replace(/,/, '');//结算价
            var oTicketSellPrice = $("#oTicketSellPrice").val().replace(/,/, '');//实收价

            //利润=实收-结算
            var oTicketProfit = oTicketSellPrice - oTicketSettlementPrice;
            $("#oTicketProfit").val(oTicketProfit);
        },
        getTime : function(value, row){
            if(value==null || value==''){
                return "";
            }
            var date = new Date(value);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            Y = date.getFullYear() + '-';
            M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            D = date.getDate() + ' ';
            return Y+M+D;
         },
         orderNoFormatter:function(value,row){
             var html = value+"<br/>";
             html += '<a href="javascript:Order.updateOrderDialog(\'' + row.oId + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp;';
             html += '<a href="javascript:Order.deleteOrderDialog(\'' + row.oId + '\')" class="btn btn-default btn-xs">删除</a>';
             return html;
         }
    };
    return t;
}();


